package com.cskaoyan.service;

import com.cskaoyan.util.ProxyUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author stone
 * @date 2022/11/11 09:29
 */
public class UserServiceProxyFactoryBean implements FactoryBean<UserService>, ApplicationContextAware {

    /*UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }*/

    String beanName;//userService
    ApplicationContext applicationContext;

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public UserService getObject() throws Exception {
        //UserService serviceProxy = ProxyUtil.getServiceProxy(UserServiceImpl.class);
        UserService userService = (UserService) applicationContext.getBean(beanName);

        UserService serviceProxy = ProxyUtil.getServiceProxy(userService);
        return serviceProxy;
    }

    @Override
    public Class<?> getObjectType() {
        return UserService.class;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
